package com.orange.cetback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.orange.cetback.pojo.entity.Questions;
import com.orange.cetback.mapper.QuestionsMapper;
import com.orange.cetback.service.QuestionOptionsService;
import com.orange.cetback.service.QuestionsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenenjun
 * @since 2025-02-16
 */
@Service
public class QuestionsServiceImpl extends ServiceImpl<QuestionsMapper, Questions> implements QuestionsService {

    @Autowired
    private QuestionOptionsService questionOptionsService;

    //根据材料获取题目
    @Override
    public List<Questions> getQuestionsByMaterialId(Integer materialId) {
        LambdaQueryWrapper<Questions> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Questions::getMaterialId, materialId);
        List<Questions> questions = this.baseMapper.selectList(wrapper);
        return questions;
    }



    //根据材料获取题目和选项
    @Override
    public List<Questions> getQuestionsOptionsByMaterialId(Integer materialId) {
        LambdaQueryWrapper<Questions> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Questions::getMaterialId, materialId);
        List<Questions> questions = this.baseMapper.selectList(wrapper);
        for (Questions question : questions){
            question.setOptions(questionOptionsService.OptionsByQuestionId(question.getQuestionId()));
        }
        return questions;
    }

    /**
     * 批量保存题目
     * @param questions
     */
    @Override
    public void saveQuestions(List<Questions> questions) {
        for (Questions question: questions) {
            this.baseMapper.insert(question);
            question.getOptions().forEach(questionOptions -> questionOptions.setQuestionId(question.getQuestionId()));
            questionOptionsService.saveBatch(question.getOptions());
        }
    }
}
